package com.wuzao.stringTest;


public class StringTest {


    public static void main(String[] args) {
        // 1. 创建字符串
        String str1 = "Hello, World!";  // 字符串字面量
        String str2 = new String("Java String API");  // 使用构造函数创建字符串

        // 2. 字符串长度
        int length = str1.length();  // 获取字符串的长度
        System.out.println("Length of str1: " + length);

        // 3. 字符串拼接
        String str3 = str1 + " " + str2;  // 使用 "+" 运算符拼接字符串
        System.out.println("Concatenated String: " + str3);

        // 4. 字符串比较
        String str4 = "hello";
        boolean isEqual = str1.equals(str4);  // 比较字符串内容是否相等
        System.out.println("str1 equals str4: " + isEqual);

        boolean isEqualIgnoreCase = str4.equalsIgnoreCase("HELLO");  // 忽略大小写比较
        System.out.println("str4 equalsIgnoreCase 'HELLO': " + isEqualIgnoreCase);

        // 5. 字符串查找
        int indexOfWorld = str1.indexOf("World");  // 查找子字符串第一次出现的位置
        System.out.println("Index of 'World' in str1: " + indexOfWorld);

        // 6. 字符串替换
        String replacedString = str1.replace("World", "Java");  // 替换子字符串
        System.out.println("Replaced String: " + replacedString);

        // 7. 字符串切割
        String[] words = str1.split(", ");  // 按指定分隔符切割字符串
        System.out.println("Words in str1:");
        for (String word : words) {
            System.out.println(word);
        }

        // 8. 字符串转换
        String upperCaseStr = str1.toUpperCase();  // 转换为大写
        String lowerCaseStr = str2.toLowerCase();  // 转换为小写
        System.out.println("Uppercase str1: " + upperCaseStr);
        System.out.println("Lowercase str2: " + lowerCaseStr);

        // 9. 去除字符串两端的空格
        String str5 = "  Hello Java  ";
        String trimmedStr = str5.trim();  // 去除字符串两端的空格
        System.out.println("Trimmed String: '" + trimmedStr + "'");

        // 10. 字符串包含
        boolean containsJava = str2.contains("Java");  // 检查字符串是否包含某个子字符串
        System.out.println("str2 contains 'Java': " + containsJava);

        // 11. 字符串转换为字符数组
        char[] charArray = str1.toCharArray();  // 将字符串转换为字符数组
        System.out.print("Character Array: ");
        for (char c : charArray) {
            System.out.print(c + " ");
        }
        System.out.println();

        // 12. 字符串的子字符串
        String subStr = str1.substring(7, 12);  // 获取从索引 7 到 12（不包括 12）的子字符串
        System.out.println("Substring of str1: " + subStr);


        int a = 1;
        System.out.println(Integer.valueOf(a));
    }


}
